package org.origin.ferret.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.origin.centre.model.base.XPage;
import org.origin.centre.model.dto.XDeleteByIdDto;
import org.origin.centre.model.dto.XQueryByIdDto;
import org.origin.centre.model.dto.XUpdateByIdDto;
import org.origin.centre.model.dto.dict.XQueryByTreeValueDto;
import org.origin.centre.model.enums.result.RS;
import org.origin.centre.model.result.R;
import org.origin.centre.model.vo.XPageVo;
import org.origin.centre.model.vo.dict.XTreeKeyValueVo;
import org.origin.ferret.system.model.dto.role.SysRoleInsertDto;
import org.origin.ferret.system.model.dto.role.SysRolePageDto;
import org.origin.ferret.system.model.dto.role.SysRoleUpdateDto;
import org.origin.ferret.system.model.vo.role.SysRoleVo;
import org.origin.ferret.system.service.SysRoleService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色-接口
 *
 * @author ferret
 * @version 2024-05-08
 */
@RestController
@Tag(name = "系统角色-接口")
@RequiredArgsConstructor
@RequestMapping("${origin.api:}/sys/role")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class SysRoleController {
    private final SysRoleService roleService;

    @PostMapping("page")
    @Operation(summary = "1.分页数据", description = "分页数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:page')")
    public R<XPageVo<SysRoleVo>> page(@Valid @RequestBody XPage<SysRolePageDto> pageDto) {
        return R.ok(RS.OK, this.roleService.page(pageDto));
    }

    @GetMapping("query")
    @Operation(summary = "2.查询数据", description = "查询数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:query')")
    public R<SysRoleVo> query(@Valid @ParameterObject XQueryByIdDto queryDto) {
        return R.ok(RS.OK, this.roleService.query(queryDto));
    }

    @PutMapping("insert")
    @Operation(summary = "3.插入数据", description = "插入数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:insert')")
    public R<Boolean> insert(@Valid @RequestBody SysRoleInsertDto insertDto) {
        return R.ok(RS.OK, this.roleService.insert(insertDto));
    }

    @PostMapping("update")
    @Operation(summary = "4.更新数据", description = "更新数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:update')")
    public R<Boolean> update(@Valid @RequestBody SysRoleUpdateDto updateDto) {
        return R.ok(RS.OK, this.roleService.update(updateDto));
    }

    @DeleteMapping("delete")
    @Operation(summary = "5.删除数据", description = "删除数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:delete')")
    public R<Boolean> delete(@Valid @ParameterObject XDeleteByIdDto deleteDto) {
        return R.ok(RS.OK, this.roleService.delete(deleteDto));
    }

    @PostMapping("valid")
    @Operation(summary = "6.生效数据", description = "生效数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:valid')")
    public R<Boolean> valid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.roleService.valid(updateDto));
    }

    @PostMapping("invalid")
    @Operation(summary = "7.失效数据", description = "失效数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:invalid')")
    public R<Boolean> invalid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.roleService.invalid(updateDto));
    }

    @PostMapping("list")
    @Operation(summary = "8.列表数据", description = "列表数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:role:list')")
    public R<List<XTreeKeyValueVo>> list(@Valid @RequestBody XQueryByTreeValueDto queryDto) {
        return R.ok(RS.OK, this.roleService.list(queryDto));
    }

}
